var Stats = function()
{
    var MAX_ATTEMPTS = 3;

    var _isBaseURL = "";
    var _requireFlashStatsAttemptsCounter = 0;

    var _statsFlashName = 'statsFlash';
    var _iSpringTracker = null;

    var _isInitialized = false;
    var _queue = []; // the queue of command to be executed

    function _doProcessQueue()
    {
        var tracker = Stats.getiSpringTracker();
        // check if valid tracker returned
        if ( !tracker )
        {
            return;
        }

        while (_queue.length != 0)
        {
            try
            {
                var args = _queue.shift();
                // first element contains method name
                var commandName = Array.prototype.shift.apply(args);
                tracker[commandName].apply(tracker, args);
            }
            catch (err)
            {
            }
        }
    }

    /**
     * Tries to load flash movie for restoring user cookie first. If it can't be
     * loaded or time out occured, processes command queue.
     */
    function _processQueue()
    {
        if ( !_isInitialized )
        {
            var flashStats = Flash.getFlashApp( _statsFlashName );
            if (  flashStats && flashStats.isLoaded != undefined || _requireFlashStatsAttemptsCounter >= MAX_ATTEMPTS )
            {
                _isInitialized = true;
            }
            else if ( _requireFlashStatsAttemptsCounter < MAX_ATTEMPTS )
            {
                ++_requireFlashStatsAttemptsCounter;
                setTimeout( _processQueue, 200 );
            }
        }

        if ( _isInitialized )
        {
            _doProcessQueue();
        }
    }

    /**
     * Method accepts unlimited number of parameters, where the first parameter
     * is the name of iSpringTracker method to call and the others are parameters
     * of that method.
     */
    function _exec()
    {
        if (arguments.length > 0)
        {
            // push command to queue, turn arguments object into an array
            _queue[_queue.length] = Array.prototype.slice.call(arguments, 0);

            _processQueue();
        }
    };

    _isInitialized = !Flash.detectFlashVer(9,0,0);

    return {
        trackPageView: function()
        {
            _exec('trackPageView');
        },

        trackEvent: function( eventName, productName, productVersion )
        {
            _exec('trackEvent', eventName, productName, productVersion);
        },

        /**
         * Inserts iSpring Stats helper flash.
         */
        insertStatsFlash: function()
        {
            var contentServerUrl = $("#content_server_url").val();

            var flashHTML = Flash.getPlayerEmbed( "statsFlash", contentServerUrl + "/resources/stats1.swf", {}, 1, 1 );
            $("#stats_flash_container").attr("innerHTML", flashHTML );
        },

        tryInitIsBaseURL: function()
        {
            if ( typeof(isBaseURL) !== 'undefined' )
            {
                _isBaseURL = isBaseURL;
            }
        },

        getiSpringTracker: function()
        {
            var typeOfTracker = (typeof(_iSpringTracker) );
            if ( typeOfTracker == 'undefined' || _iSpringTracker == null )
            {
                try
                {
                    if ( typeof(iSpring) != 'undefined' && _isBaseURL != "" )
                    {
                        _iSpringTracker = iSpring.getTracker( _isBaseURL, 5 );
                    }
                }
                catch(err)
                {
                }
            }
            return _iSpringTracker;
        }
    };
}();

Stats.tryInitIsBaseURL();

$(function(){
    Stats.insertStatsFlash();
    Stats.trackPageView();
});